//
//  Data01ViewController.m
//  iPhoneDev02
//
//  Created by smu on 11-7-24.
//  Copyright 2011年 duk. All rights reserved.
//

#import "Data01ViewController.h"
#import "CoreData01AppDelegate.h"
#import "CoreData01.h"

@implementation Data01ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    CoreData01AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;

    //////////////增加
    CoreData01 *coreData01 = (CoreData01 *)[NSEntityDescription 
                                            insertNewObjectForEntityForName:@"CoreData01" inManagedObjectContext:context];
    coreData01.title = @"标题7";
    //保存
    if (![context save:&error]) {
        NSLog(@"Error %@",[error localizedDescription]);
    }
    
    ////////查询
    //相当于 "select * from xxx" 中的 xxx 部分
    NSEntityDescription *entityDescription = [NSEntityDescription
                                              entityForName:@"CoreData01" 
                                              inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    
    [request setEntity:entityDescription];
    
    //where xxx
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(id = %i)", 0];
    [request setPredicate:pred];
    
    
     
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects == nil) {
        NSLog(@"There was an error!");
    }
    
    for (NSManagedObject *oneObject in objects) {
        NSLog(@"标题:%@",[oneObject valueForKey:@"title"]);
    }
    [request release];
    
    /////////删除
    NSFetchRequest *requestDel = [[NSFetchRequest alloc] init];
    [requestDel setEntity:entityDescription];
    
    NSPredicate *predDel = [NSPredicate predicateWithFormat:@"(title = %@)", @"标题6"];
    [requestDel setPredicate:predDel];
    
    NSArray *objectsDel = [context executeFetchRequest:requestDel error:&error];
    if (objectsDel == nil) {
        NSLog(@"没有删除的数据");
    }
    for (NSManagedObject *delObject in objectsDel) {
        [context deleteObject:delObject];
    }
    [requestDel release];

    if (![context save:&error]) {
        NSLog(@"Error删除保存 %@",[error localizedDescription]);
    }
    
    ///////改变
    NSFetchRequest *requestEdit = [[NSFetchRequest alloc] init];
    [requestEdit setEntity:entityDescription];
    NSPredicate *preEdit = [NSPredicate predicateWithFormat:@"(title = %@)",@"标题5"];
    [requestEdit setPredicate:preEdit];
    NSArray *objectsEdit = [context executeFetchRequest:requestEdit error:&error];
    if (objectsEdit == nil) {
        NSLog(@"修改没有数据");
    }
    for (NSManagedObject *editObject in objectsEdit) {
        [editObject setValue:@"修改" forKey:@"title"];
    }
    if (![context save:&error]) {
        NSLog(@"Error修改保存 %@",[error localizedDescription]);
    }
    
    
    
//    if () {
//        <#statements#>
//    }
//    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
